// 分类模块
import { topCategory } from "@/api/constants";
import { findAllCategory } from "@/api/category";
export default {
  namespaced: true,
  state: () => {
    return {
      // 分类信息
      list: topCategory.map((item) => ({ name: item })),
    };
  },
  mutations: {
    setList(state, payload) {
      state.list = payload;
    },
    // 定义show和hide函数，控制当前分类的二级分类显示和隐藏
    show(state, id) {
      const curCategory = state.list.find((item) => item.id === id);
      curCategory.open = true;
    },
    hide(state, id) {
      const curCategory = state.list.find((item) => item.id === id);
      curCategory.open = false;
    },
  },
  actions: {
    async getList({ commit }) {
      const data = await findAllCategory();
      // 给每个分类加上控制二级分类显示隐藏的数据
      data.result.forEach((top) => {
        top.open = false;
      });
      commit("setList", data.result);
    },
  },
};
